<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mahasiswa extends Controller {

	function Mahasiswa()
	{
		parent::Controller();
		$this->load->library('auth');
		$this->load->library('fungsi');
		$this->load->model(array('setting_m', 'area_m'));
		$this->load->model('app_login_auth');
	}	
	function index()
	{
		$this->data['graph'] = $this->creategraph();
		if ($this->session->userdata('login') == false)
		{
			redirect('home');
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 1)
		{
			redirect('admin');
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 3)
		{
			redirect('dosen');
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 4)
		{	
			//Mengambil 10 data koleksi terbaru yang di upload
			$this->data['newUpload'] = $this->setting_m->getNewUpload('');
			//content body
			$this->data['content'] = 'home/home_v';
			$this->data['left'] = 'bg_left';
			$this->data['footer'] = 'footer';
			$this->data['account'] = $this->setting_m->getUploadFromTypeCollection();
			$this->data['induk'] = $this->session->userdata('ID');
			
			//Memanggil View
			$this->load->view('home/index', $this->data);
		}
	}
	function insert()
	{
		//content body
		$this->data['content'] = 'mahasiswa/insert';
		$this->data['left'] = 'bg_left';
		$this->data['footer'] = 'footer';
		$this->data['account'] = $this->setting_m->getUploadFromTypeCollection();
		//header
		$this->data['header'] = 'bg_header';
		//Mengambil 10 data jenis koleksi yang baru diupload
		$this->data['newTypeUpload'] = $this->setting_m->getUploadFromTypeCollection();
			
		//Untuk Binding data ke dropdownlist
		//'ddlJenisKol' dikirim ke view untuk memasukan data ke dalam dropdownlist
		$this->data['ddlJenisKol'] = $this->setting_m->getCboJenisKoleksi();
		$this->data['ddlSubTipeKol'] = $this->setting_m->getCboSubTypeCol();
		$this->data['ddlJurusan'] = $this->setting_m->getCboJurusan();
		$this->data['ddlPembimbing'] = $this->setting_m->getCboPembimbing();
		$this->data['induk'] = $this->session->userdata('ID');
		$this->data['nama'] = $this->session->userdata('name');
		$this->data['error'] = '';
		$this->data['tableCollection']= $this->getTableAllCollection($this->session->userdata('ID'));
		
		//Memanggil View
		$this->load->view('home/index', $this->data);
	}
	function do_Saving($dataUpload)
	{
		$Title       = $this->input->post('txtTitle', TRUE);
        $Abstract    = $this->input->post('txtAbstract', TRUE);
		$jnsKoleksi  = $this->input->post('ddlJenisKoleksiInsert', TRUE);
		$noAuthor = $this->input->post('txtNoAuthor', TRUE);
		$nameAuthor = $this->input->post('txtAuthor', TRUE);
		$jabAuthor = $this->input->post('txtJabtAuth', TRUE);
		$noAdvisor = $this->input->post('txtNoAdivisor', TRUE);
		$nameAdvisor = $this->input->post('txtAdivisor', TRUE);
		$jabAdvisor = $this->input->post('txtJabtAdivisor', TRUE);
		$jurusan  = $this->input->post('ddlJurusanInsert', TRUE);
		$subtipekol  = $this->input->post('ddlJenisTipeKoleksiInsert', TRUE);
		$tglRealese  = $this->input->post('dtRelaseMhs', TRUE);
		
		try
		{
			if ( $result = $this->setting_m->SavedCollection($Title, $Abstract, $jnsKoleksi, $noAuthor, $nameAuthor, $noAdvisor, $nameAdvisor, $jurusan, $tglRealese,$dataUpload,$subtipekol,$jabAuthor,$jabAdvisor) == true)
			{
				return true;
			}
		}
		catch (Exception $e)   // You throwed your custom exception with the failed validation information
		{
			// Do something with your custom exception like the kohana example
			$this->data['error'] = 'Data tidak berhasil diupload';
			unlink("./uploads/".$dataUpload['file_name']);
			$this->load->view('mahasiswa/insert', $this->data);
		}
	}
	function do_upload()
	{
		
		$this->load->library('form_validation');
		$this->data['content'] = 'mahasiswa/insert';
		$this->data['left'] = 'bg_left';
		$this->data['footer'] = 'footer';
		$this->data['account'] = $this->setting_m->getUploadFromTypeCollection();
		//header
		$this->data['header'] = 'bg_header';
		//Mengambil 10 data jenis koleksi yang baru diupload
		$this->data['newTypeUpload'] = $this->setting_m->getUploadFromTypeCollection();
			
		//Untuk Binding data ke dropdownlist
		//'ddlJenisKol' dikirim ke view untuk memasukan data ke dalam dropdownlist
		$this->data['ddlSubTipeKol'] = $this->setting_m->getCboSubTypeCol();
		$this->data['ddlJenisKol'] = $this->setting_m->getCboJenisKoleksi();
		$this->data['ddlJurusan'] = $this->setting_m->getCboJurusan();
		$this->data['ddlPembimbing'] = $this->setting_m->getCboPembimbing();
		$this->data['induk'] = $this->session->userdata('ID');
		$this->data['nama'] = $this->session->userdata('name');
		$this->data['error'] = '';
		
		
		//Get header
		$this->data['header'] = 'bg_header_mhs';
		
		$configValidation = array(
							array(
                                 'field'   => 'txtTitle', //nama elemen form
                                 'label'   => 'Title', //keterangan form
                                 'rules'   => 'required' //harus di isi,minimal 10 angka,integer
                              ),
							array(
                                 'field'   => 'txtAbstract',
                                 'label'   => 'Abstract',
                                 'rules'   => 'required'
                              ),
							array(
                                 'field'   => 'ddlJenisKoleksiInsert',
                                 'label'   => 'Jenis Koleksi',
                                 'rules'   => 'required'
                              ),   
							array(
                                 'field'   => 'txtNoAuthor',
                                 'label'   => 'Nomor Induk Penulis',
                                 'rules'   => 'required'
                              ),
							array(
                                 'field'   => 'ddlJurusanInsert',
                                 'label'   => 'Jurusan',
                                 'rules'   => 'required'
                              ),
							array(
                                 'field'   => 'dtRelaseMhs',
                                 'label'   => 'Tgl. Release',
                                 'rules'   => 'required'
                              )
                        );
		
		$this->form_validation->set_rules($configValidation); 
        if($this->form_validation->run()!==false){
			$configUpload['upload_path'] = './uploads/';
			$configUpload['allowed_types'] = 'doc|docx|pdf|rar|zip';
			$configUpload['max_size']  = '9000';
		 
			$this->load->library('upload', $configUpload);
				// modifikasi disini
				// looping $_FILES dan buat array baru
				foreach($_FILES['userfile'] as $key=>$val)
				{
					$i = 1;
					foreach($val as $v)
					{
						$field_name = "file_".$i;
						$_FILES[$field_name][$key] = $v;
						$i++;
					}
				}
				// hapus array awal, karena kita sudah memiliki array baru
				unset($_FILES['userfile']);
			 
				// variabel error diubah, dari string menjadi array
				$error = array();
				$success = array();
				foreach($_FILES as $field_name => $file)
				{
					if ( ! $this->upload->do_upload($field_name))
					{
						$error[] = $this->upload->display_errors();
					}
					else
					{
						$success[] = $this->upload->data();
					}
				}
				//Jika tidak berhasil upload
				if(count($error) > 0)
				{
					$this->data['error'] = false;
					$this->data['msg'] = implode('<br />',$error);
					$this->load->view('home/index', $this->data);
				}
				else
				{
				$data = $success;
					$pathApp = base_url();
					// //Jika berhasil upload, lakukan saving
					if ($this->do_saving($data) == true)
					{
						$this->data['tableCollection']= $this->getTableAllCollection($this->session->userdata('ID'));
						$this->data['error'] = false;
						$this->data['msg'] = 'Data Berhasil Disimpan';
						$this->load->view('home/index', $this->data);
					}
				}
		}
		else
		{
			$this->data['tableCollection']= $this->getTableAllCollection($this->session->userdata('ID'));
			$this->data['error'] = true;
			$this->load->view('home/index', $this->data);
		}
	}
	function deleteKoleksi($id)
	{
		try
		{
			$result = $this->setting_m->deleteKoleksi($id);
			$this->data['error'] = 'Data Berhasil di Hapus';
			redirect('mahasiswa/insert');
		}
		catch (Exception $e)   // You throwed your custom exception with the failed validation information
		{
			// Do something with your custom exception like the kohana example
			//Memanggil View
			$this->data['error'] = 'Gagal delete';
			redirect('mahasiswa/insert');
		}
	}
	private function getTableAllCollection($nip)
	{
		$dtTable = '';
		// Load data dari tabel absen
		$dtTable = $this->setting_m->getCollectionPerNIP($this->session->userdata("ID"));
			
			$this->data['tableCollection'] = $dtTable;
		return $dtTable;
	}
		
	function creategraph()
	{
		$this->load->library('graph');
		$data_1 = array('30','20');
		// for( $i=0; $i<7; $i++ )
		// {
			// $data_1[] = rand(30,0);
		// }
		$ff = new graph();
		$ff->set_data( $data_1 );
		$ff->title( 'Trafik Jumlah Koleksi', '{font-size: 14px; color: #3D5570;font-family:calibri;}' );
		$ff->line_dot( 3, 5, '#8B6122', 'Repository', 10 );
		$ff->bg_colour = '#FFFFFF';
		$ff->x_axis_colour( '#818D9D', '#ADB5C7' );
		$ff->y_axis_colour( '#818D9D', '#ADB5C7' );
		$ff->set_x_labels( array( '2013') );
		$ff->set_y_max( 600 );
		$ff->y_label_steps( 10 );
		$ff->set_y_legend( 'Jumlah Koleksi', 12, '#3D5570' );
		$ff->set_x_legend( 'Tahun', 12, '#3D5570' );
		$ff->set_output_type('js');
		$ff->width = '20%';
		$ff->height = '20%';
		return $ff->render();
	}
	
}